﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain;
using Data.Repositories;
using System.Web.Security;

namespace CantinhodaCegonha.Controllers
{ 
    public class RespostaController : Controller
    {
        private ModeloCantinhoContainer db = new ModeloCantinhoContainer();

        //
        // GET: /Resposta/
        
        [ChildActionOnly]
        public ViewResult Index()
        {
            var answer = db.Answer.Include("Question").Include("User");
            return View(answer.ToList());
        }

        //
        // GET: /Resposta/Details/5

        public ViewResult Details(int id)
        {
            Answer answer = db.Answer.Single(a => a.Id == id);
            return View(answer);
        }


        public JsonResult SaveComment(string comment,string parent, int questionId)
        {
            RespostaRepository rep = new RespostaRepository();
            return Json(new { id = rep.GravaComentario(comment, parent, User.Identity.Name, questionId), nome = ((FormsIdentity)User.Identity).Ticket.UserData });
        }

        //
        // GET: /Resposta/Create

        public ActionResult Create()
        {
            ViewBag.QuestionId = new SelectList(db.Question, "Id", "Title");
            ViewBag.UserId = new SelectList(db.User, "Id", "Email");
            return View();
        } 

        //
        // POST: /Resposta/Create

        [HttpPost]
        public ActionResult Create(Answer answer)
        {
            if (ModelState.IsValid)
            {
                db.Answer.AddObject(answer);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.QuestionId = new SelectList(db.Question, "Id", "Title", answer.QuestionId);
            ViewBag.UserId = new SelectList(db.User, "Id", "Email", answer.UserId);
            return View(answer);
        }
        
        //
        // GET: /Resposta/Edit/5
 
        public ActionResult Edit(int id)
        {
            Answer answer = db.Answer.Single(a => a.Id == id);
            ViewBag.QuestionId = new SelectList(db.Question, "Id", "Title", answer.QuestionId);
            ViewBag.UserId = new SelectList(db.User, "Id", "Email", answer.UserId);
            return View(answer);
        }

        //
        // POST: /Resposta/Edit/5

        [HttpPost]
        public ActionResult Edit(Answer answer)
        {
            if (ModelState.IsValid)
            {
                db.Answer.Attach(answer);
                db.ObjectStateManager.ChangeObjectState(answer, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.QuestionId = new SelectList(db.Question, "Id", "Title", answer.QuestionId);
            ViewBag.UserId = new SelectList(db.User, "Id", "Email", answer.UserId);
            return View(answer);
        }

        //
        // GET: /Resposta/Delete/5
 
        public ActionResult Delete(int id)
        {
            Answer answer = db.Answer.Single(a => a.Id == id);
            return View(answer);
        }

        //
        // POST: /Resposta/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Answer answer = db.Answer.Single(a => a.Id == id);
            db.Answer.DeleteObject(answer);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpPost]
        [Authorize]
        public JsonResult Vote(int id,int action)
        {
            if (Request.IsAjaxRequest())
            {
                if (this.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains(User.Identity.Name + "Votou-" + id))
                {
                    return Json(new { id = id, votos = 0, success = "javotou" });
                }
                else
                {
                    HttpCookie cookie = new HttpCookie(User.Identity.Name + "Votou-" + id);
                    cookie.Value = "Votou";
                    cookie.Expires = DateTime.MaxValue;
                    HttpContext.Response.Cookies.Add(cookie);

                    RespostaRepository resp = new RespostaRepository();
                    int voto = resp.Votar(id, action, User.Identity.Name);
                    string sucesso = "true";
                    if (voto == -10000)
                        sucesso = "false";
                    if (voto == -10001)
                        sucesso = "votoproprio";
                    return Json(new { id = id, votos = voto, success = sucesso });
                }
            }
            return Json(new { id = id, votos = 0, success = "false" });
        }

        [HttpPost]
        [Authorize]
        public JsonResult MarcarCorrecta(int idresp, int idperg)
        {
            return Json(new {lastmarcado=123, idresp = idresp, idperg = idperg, success = "true" });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}